﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Data;
using JXT.PrimaryKey.Batman.Extensions;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Models;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Repositories;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql.Expressions;

namespace JXT.PrimaryKey.Batman.Domain.Repositories.UserSystem
{
    public class MySqlSystemUserRepository : MySqlRepositoryBase<SystemUser, int>, ISystemUserRepository
    {
        public MySqlSystemUserRepository(RepositoryContext context)
            : base((MySqlRepositoryContext)context)
        {
        }

        public void AddStatus(SystemStatus status)
        {
            InsertQuery insertQuery = new InsertQuery();
            insertQuery.Append(status);
            object key = Context.ExecuteScalar(insertQuery.QueryText, insertQuery.Parameters);
        }

        public void DeleteStatus(string sysId)
        {
            string sql = "delete from `systemstatus` where `sysid`=@sysid";
            Context.ExecuteNonQuery(sql, new[] { Context.CreateParameter("@sysid", sysId) });
        }

        public SystemStatus GetStatus(string sysId)
        {
            string sql = "select * from `systemstatus` where `sysid`=@sysid";
            SystemStatus result = null;
            using (var dr = Context.ExecuteDataReader(sql, new[] { Context.CreateParameter("@sysid", sysId) }))
            {
                if (dr.Read())
                {
                    result = ReadFromDataReader<SystemStatus>(dr);
                }
            }
            return result;
        }

        public void UpdateStatus(SystemStatus status)
        {
            UpdateQuery updateQuery = new UpdateQuery("sysid");
            updateQuery.Append(status);
            Context.ExecuteNonQuery(updateQuery.QueryText, updateQuery.Parameters);
        }
    }
}
